package com.java.feature.handle;

import com.java.feature.strategy.FunctionStrategy;

import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

/**
 * 函数枚举句柄
 *
 * @author Amadeus
 * @date 2024/04/26
 */
public enum FunctionHandleEnum implements FunctionStrategy<String, String, String> {

    /**
     * 任务
     */
    TASK("0"::equals, message -> "任务开始:" + message),

    /**
     * 产品
     */
    PRODUCT("1"::equals, message -> "产品线开始:" + message),
    ;

    private final Predicate<String> predicate;
    private final Function<String, String> function;

    FunctionHandleEnum(Predicate<String> predicate, UnaryOperator<String> function) {
        this.predicate = predicate;
        this.function = function;
    }

    @Override
    public Predicate<String> predicate() {
        return predicate;
    }

    @Override
    public Function<String, String> function() {
        return function;
    }

    public Predicate<String> getPredicate() {
        return predicate;
    }

    public Function<String, String> getFunction() {
        return function;
    }

    public static String error(String code) {
        for (FunctionHandleEnum value : FunctionHandleEnum.values()) {
            if (value.predicate.test(code)) {
                return value.function.apply(code);
            }
        }
        return null;
    }
}
